﻿using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExcelAddIn
{
    public partial class QiuTong : Form
    {
        Microsoft.Office.Interop.Excel.Application app = Globals.ThisAddIn.Application;

        public QiuTong()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSrc_Click(object sender, EventArgs e)
        {
            var selectRang = app.InputBox("数据源", "请选择数据源，按住Ctrl键可多选", Type: 8);
            if (selectRang is Range)
            {
                InputeSrc.Text = ((Range)selectRang).Address;
            }
        }
        /// <summary>
        /// 匹配区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDic_Click(object sender, EventArgs e)
        {
            var selectRang = app.InputBox("匹配区域", "请选择匹配区域，按住Ctrl键可多选", Type: 8);
            if (selectRang is Range)
            {
                InputeDic.Text = ((Range)selectRang).Address[External: true];
            }
        }

        /// <summary>
        /// 开始匹配
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(InputeSrc.Text))
            {
                MessageBox.Show("请选择数据源");
                return;
            }

            if (string.IsNullOrEmpty(InputeDic.Text))
            {
                MessageBox.Show("请选择目标区域");
                return;
            }
            Color selectColor = buttonColor.BackColor;
            //目标区域
            Range dicRang = ((Worksheet)(app.ActiveSheet)).Range[InputeDic.Text];
            Range srcRang = ((Worksheet)(app.ActiveSheet)).Range[InputeSrc.Text];
            var isBack = radioButton1.Checked;
            var isSame = radioButtonSame.Checked;
            if (isSame)
                GetSame(srcRang, dicRang, isBack);
            else
                GetDiffer(srcRang, dicRang, isBack);
        }

        public void GetSame(Range srcRang, Range dicRang,bool isBack)
        {
            Color selectColor = buttonColor.BackColor;
            bool isExact = checkBoxExact.Checked;//是否精确匹配
            foreach (Range cell in srcRang.Cells)
            {
                if (cell.Text == null || string.IsNullOrEmpty(cell.Text))
                    continue;

                Range res ;
                if(isExact)
                    res= dicRang.Find(cell.Value);
                else
                    res= dicRang.Find(cell.Value, MatchCase:true);

                string firstAddress = "";
                int findCount = 0;
                Int32.TryParse(textBoxCount.Text, out findCount);
                int turn = 0;
                while (res != null && res.Address != firstAddress && (findCount==0|| turn++< findCount ))
                {
                    if (string.IsNullOrEmpty(firstAddress))
                        firstAddress = res.Address;
                    res = dicRang.FindNext(After: res);

                    if (isBack)
                        res.Interior.Color = selectColor;
                    else
                        res.Font.Color = selectColor;
                };
            }
        }
        public void GetDiffer(Range srcRang, Range dicRang, bool isBack)
        {
            Color selectColor = buttonColor.BackColor;
            bool isExact = checkBoxExact.Checked;
            //设置所有颜色
            foreach (Range cell in dicRang.Cells)
            {
                if (cell.Text == null || string.IsNullOrEmpty(cell.Text))
                    continue;

                if (isBack)
                    cell.Interior.Color = selectColor;
                else
                    cell.Font.Color = selectColor;
            }
            //反向设置空颜色
            foreach (Range cell in srcRang.Cells)
            {
                if (cell.Text == null || string.IsNullOrEmpty(cell.Text))
                    continue;

                Range res;
                if (isExact)
                    res = dicRang.Find(cell.Value);
                else
                    res = dicRang.Find(cell.Value, MatchCase: true);
                string firstAddress = "";
                int findCount = 0;
                Int32.TryParse(textBoxCount.Text, out findCount);
                int turn = 0;
                while (res != null && res.Address != firstAddress && (findCount == 0 || turn++ < findCount))
                {
                    if (string.IsNullOrEmpty(firstAddress))
                        firstAddress = res.Address;
                    res = dicRang.FindNext(After: res);

                    if (isBack)
                        res.Interior.ColorIndex = XlColorIndex.xlColorIndexNone;
                    else
                        res.Font.Color =Color.Black;
                };
            }
        }

        /// <summary>
        /// 颜色选择器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            buttonColor.BackColor = colorDialog1.Color;
        }
    }
}
